Regular Expressions


Regular Expressions

Regular expressions allow easily matching, finding and replacing text. A regular expression is composed of text using a special language as shown in the following table.
Las expresiones regulares permiten fácilmente verificar, encontrar y reemplazar texto. Una expresión regular está compuesta de texto usando un lenguaje especial como se muestra en la tabla siguiente.

Summary

Tip

Un programa debe incluir (editando el archivo stdafx.h) la librería regex para poder usar las expresiones regulares como se muestra en la figura de abajo.

stdafx

Problem 1
Create a program called TestRegex to test the regular expressions of this section. Do not forget to include the library regex in the file stdafx.h
Cree un programa llamado TestRegex para probar las expresiones regulares de esta sección. No se olvide de incluir la librería regex en el archivo stdafx.h

TestRegex

TestRegex.cpp
void TestRegex::Window_Open(Win::Event& e)
{
}

void TestRegex::btMatch_Click(Win::Event& e)
{
}


Problem 2
Create a regular expression to match a string with one single digit.
Cree una expresión regular para verificar que una cadena de texto es un solo digito.

Input    Match  
3yes
10no
6yes
-3no
abcno
3fno

TestRegex1

TestRegex2

TestRegex.cpp
void TestRegex::Window_Open(Win::Event& e)
{
}

void TestRegex::btMatch_Click(Win::Event& e)
{
     wregex reg(L"[0123456789]");

     if (regex_match(tbxInput.Text, reg) == true)
     {
          tbxInput.ShowBalloonTip(L"TestRegex", L"The string matches", TTI_NONE);
     }
     else
     {
          tbxInput.ShowBalloonTip(L"TestRegex", L"The string does NOT match", TTI_ERROR);
     }
}


Problem 3
Create a regular expression to match a string that start with the letter a, and follows of two digits. Answer: a[0-9][0-9]
Cree una expresión regular para verificar que una cadena de texto empiece con una letra a y siga de dos dígitos. Respuesta: a[0-9][0-9]

Input    Match  
a3no
A34no
A567no
Aa78no
b79no
b3afno
a56yes
a562no

Problem 4
Create a regular expression to match a string with numbers from 100 to 599.
Cree una expresión regular para verificar que una cadena de texto con números del 100 al 599.

Input    Match  
0no
134yes
A567no
Aano
600no
550yes

Problem 5
Create a regular expression to match a negative integer number. Answer: -[0-9]+
Cree una expresión regular para verificar un número entero negativo. Respuesta: -[0-9]+

Input    Match  
0no
-134yes
A567no
56no
z 600no
55no
-15yes
-05yes

Problem 6
Create a regular expression to match: the letter A followed of one digit or the letter B followed of two digits. Answer: (A[0-9])|(B[0-9][0-9])
Cree una expresión regular para verificar el patrón: una letra A seguida de un dígito o la letra B seguida de dos dígitos. Respuesta: (A[0-9])|(B[0-9][0-9])

Input    Match  
A5yes
A54no
B567no
B56yes
Az 600no
A8yes
-15no
05no

Problem 7
Create a regular expression to match a positive integer number with the first digit on the left be different from zero. You can use the OR operator to combine two regular expression using a pipe sign: (expression1) | (expression2). As the plus sign is a reserved (special) character in the regular expression language, you must use the escape character, i.e., \\+
Cree una expresión regular para verificar un número entero positivo con el primer digito a la izquierda sea diferente de cero. Usted puede usar el operador de OR para combinar dos expresiones regulares usando el signo de pipe: (expresion1) | (expresion2). Con el signo de "mas" es un símbolo reservado en el lenguaje de las expresiones regulares, usted debe usar el símbolo de escape, por ejemplo: \\+

Input    Match  
001no
1yes
-134no
A567no
56yes
z 600no
55yes
-15no
05no
10yes
108988yes
1089Qno
+189yes
+0189no
0189no

Problem 8
Create a regular expression to match: an even number. Observe that in this case, the modulus operator is easier than using a regular expression.
Cree una expresión regular para verificar el patrón: un número par. Observe que en este caso, el operador de modulo es más fácil que una expresión regular.

Tip
In most programming languages, the backslash \ is an escape character and special considerations must be taken. Thus, when using a backslash \ inside a regular expression in a program, you must use two backslashes. For instance, for any character the regular expression is \w, thus the code must be wregex reg(L"\\w");. Similarly, the regular expression for integers is \d, thus the code must be wregex reg(L"\\d");
En la mayoría de los lenguajes de programación la diagonal invertida es un símbolo de escape y algunas consideraciones especiales deben tomarse en cuenta. Así, cuando se usa la diagonal invertida dentro de una expresión regular en un programa, usted debe usar dos diagonales invertidas. Por ejemplo, para cualquier símbolo la expresión regular es \w, así el código debe ser wregex reg(L"\\w");. En forma similar, la expresión regular para un entero es \d, así el código debe ser wregex reg(L"\\d");

Tip
It is important to note that when using a textbox to input a regular expression you do not worry about the escape character. This implies that the user never worries about escape characters.
Es importante notar que cuando se usa una caja de texto como entrada de una expresión regular usted no tiene que preocuparse del símbolo de escape. Esto implica que el usuario nunca se preocupa por símbolos de escape.

Tip
A line break may be composed of: \n or \r\n. In Microsoft Windows most documents have \r\n, however, in other platforms they have only \n. Therefore, the regular expression for line break may be \r?\n. To match the end of a line use: (?=\r?$)
Un salto de línea puede estar compuesto de: \n o \r\n. En Microsoft Windows la mayoría de los documentos tienen \r\n, sin embargo, en otras plataformas los documentos solo tienen \n. Por lo tanto, una expresión regular para salto de de línea puede ser \r?\n. Para buscar el final de una línea use: (?=\r?$)

Tip
To make a regular expression sensitive to upper and lower case use i at the end. For instance, to match a set of uppercase letters use:

/[A-Z]+/i
Para hacer una expresión regular sensible entre mayúsculas y minúsculas use i al final. Por ejemplo, para encontrar un conjunto de letras mayúsculas use:

/[A-Z]+/i

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home